// 1 多态
var makeSound = function(animal) {
  if (animal instanceof Duck) {
    console.log('嘎嘎嘎');
  } else if (animal instanceof Chicken) {
    console.log('咯咯咯');
  }
};
var Duck = function() {};
var Chicken = function() {};
makeSound(new Duck()); // 嘎嘎嘎
makeSound(new Chicken()); // 咯咯咯

/**
 * 开放封闭原则
 * 把可变的部分各自封装起来
 */
var makeSound = function(animal) {
  animal.sound();
};
var Duck = function() {}
Duck.prototype.sound = function() {
  console.log('嘎嘎嘎');
};
var Chicken = function() {}
Chicken.prototype.sound = function() {
  console.log('咯咯咯');
};
makeSound(new Duck()); // 嘎嘎嘎
makeSound(new Chicken()); // 咯咯咯

var Dog = function() {}
Dog.prototype.sound = function() {
  console.log('汪汪汪');
};
makeSound(new Dog()); // 汪汪汪


// 多态在程序设计中的作用
var googleMap = {
  show() {
    console.log('开始渲染谷歌地图');
  }
};
var baiduMap = {
  show() {
    console.log('开始渲染百度地图');
  }
}; 
// 脆弱的弹性
var renderMap = function(type) {
  if (type === 'google') {
    googleMap.show();
  } else if (type === 'baidu') {
    baiduMap.show();
  }
};
renderMap('google');
renderMap('baidu');

// 把程序中相同的部分抽象出来
var renderMap = map => {
  if(map.show instanceof Function) {
    map.show()
  }
}
renderMap(googleMap)
renderMap(baiduMap)


// 2 原型模式
var Plane = function() {
  this.blood = 100;
  this.attackLevel = 1;
  this.defenseLevel = 1;
};
var plane = new Plane();
plane.blood = 500;
plane.attackLevel = 10;
plane.defenseLevel = 7;
var clonePlane = Object.create(plane);
console.log(clonePlane);
// 输出：Object {blood: 500, attackLevel: 10, defenseLevel: 7} 
 